<template>
  <div class="box">
    <el-form ref="form" :model="form" :rules="rules" label-width="90px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="用户账号" prop="userName">
            <el-input
              disabled
              v-model="form.userName"
              placeholder="请输入用户账号"
              maxlength="30"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用户学号" prop="userId">
            <el-input
              disabled
              v-model="form.userId"
              placeholder="请输入用户学号"
              maxlength="50"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="用户昵称" prop="nickName">
            <el-input
              :disabled="readonly"
              v-model="form.nickName"
              placeholder="请输入用户昵称"
              maxlength="30"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用户专业" prop="deptId">
            <!-- <treeselect
              disabled
              v-model="form.deptId"
              :options="deptOptions"
              :show-count="true"
              placeholder="请选择归属部门"
            /> -->
            <el-input
              disabled
              v-model="deptName"
              placeholder="用户专业"
              maxlength="11"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input
              :disabled="readonly"
              v-model="form.phonenumber"
              placeholder="请输入手机号码"
              maxlength="11"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用户邮箱" prop="email">
            <el-input
              :disabled="readonly"
              v-model="form.email"
              placeholder="请输入邮箱"
              maxlength="50"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="用户性别" prop="phonenumber">
            <el-radio-group v-model="form.sex" :disabled="readonly">
              <el-radio label="0">男</el-radio>
              <el-radio label="1">女</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="操作">
            <el-button
              type="primary"
              v-show="readonly"
              @click="readonly = false"
              >编辑</el-button
            >
            <el-button
              type="primary"
              v-show="!readonly"
              @click="updateUserProfile"
              >保存</el-button
            >
            <el-button
              @click="(readonly = true), getUserInfo()"
              v-show="!readonly"
              >取消</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- <el-row>
        <el-col :span="12">
          <el-form-item label="用户密码" prop="password">
            <el-input
              :readonly="readonly"
              v-model="form.password"
              placeholder="请输入用户密码"
              type="password"
              maxlength="20"
              show-password
            />
          </el-form-item>
        </el-col>
      </el-row> -->
  </div>
</template>
<script>
import {
  updateUserProfile,
  getUserInfo,
  deptTreeSelect,
} from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  data() {
    return {
      // 部门树选项
      deptOptions: "",
      defaultProps: {
        children: "children",
        label: "label",
      },
      readonly: true, //是否只读
      // 角色选项
      roleOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" },
        ],
        email: [
          { required: true, message: "邮箱地址不能为空", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        phonenumber: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
      },
      userId: null,
      deptName: "",
    };
  },
  components: {
    Treeselect,
  },
  mounted() {
    this.deptName = this.$store.state.user.deptName;
    this.userId = this.$store.getters.userId;

    // console.log("用户id", this.$store.state.user);
    console.log("用户id", this.$store.getters.userId);
    this.getUserInfo();
    // this.getDeptTree();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        console.log(response.data);
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    //获取用户信息
    getUserInfo() {
      getUserInfo(this.userId).then((res) => {
        console.log(res);
        this.form = res.data;
      });
    },
    //更新用户信息
    updateUserProfile() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateUserProfile(this.form).then((response) => {
            this.$message.success("修改成功");
            this.readonly = true;
            this.getUserInfo();
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.box {
  padding: 30px;
}
</style>
